<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="规则名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleName">
              <a-input v-model="model.ruleName" placeholder="请输入规则名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="执行角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roleId">
              <j-dict-select-tag type="list" v-model="model.roleId" dictCode="sys_role,role_name,id" placeholder="请选择执行角色" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="执行周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executeCycle">
              <j-dict-select-tag type="list" v-model="model.executeCycle" dictCode="execute_cycle" placeholder="请选择执行周期" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="执行次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executeNum">
              <a-input-number v-model="model.executeNum" placeholder="请输入执行次数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="规则类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleType">
              <j-dict-select-tag type="list" v-model="model.ruleType" dictCode="rule_type" placeholder="请选择规则类型" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12" >
            <a-form-model-item label="线路" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lineId">
              <j-dict-select-tag type="list" v-model="model.lineId" dictCode="ins_line,line_name,id" placeholder="请选择线路" />
            </a-form-model-item>
          </a-col>-->
<!--          <a-col :span="12" >
            <a-form-model-item label="人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <j-select-user-by-dep v-model="model.userId" :multi="true" />
            </a-form-model-item>
          </a-col>-->
<!--          <a-col :span="12" >
            <a-form-model-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">
              <j-select-depart v-model="model.departId" :multi="true"  />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="12" >
            <a-form-model-item label="规则状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleStatus">
              <j-dict-select-tag type="list" v-model="model.ruleStatus" dictCode="rule_status" placeholder="请选择规则状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="线路规则" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="insRuleLineTable.loading"
          :columns="insRuleLineTable.columns"
          :dataSource="insRuleLineTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
      <a-tab-pane tab="值班规则" :key="refKeys[1]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[1]"
          :loading="insRuleDutyTable.loading"
          :columns="insRuleDutyTable.columns"
          :dataSource="insRuleDutyTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'InsRuleForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
            executeCycle:"1",
            executeNum:1,
            ruleType:"1",
            ruleStatus:"1",
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           ruleName: [
              { required: true, message: '请输入规则名称!'},
           ],
           roleId: [
              { required: true, message: '请输入执行角色!'},
           ],
           executeCycle: [
              { required: true, message: '请输入执行周期!'},
           ],
           executeNum: [
              { required: true, message: '请输入执行次数!'},
           ],
        },
        refKeys: ['insRuleLine', 'insRuleDuty', ],
        tableKeys:['insRuleLine', 'insRuleDuty', ],
        activeKey: 'insRuleLine',
        // 线路规则
        insRuleLineTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '线路',
              key: 'lineId',
              type: JVXETypes.select,
              options:[],
              dictCode:"ins_line,line_name,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '备注',
              key: 'remark',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 值班规则
        insRuleDutyTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '部门',
              key: 'deptId',
              type: JVXETypes.departSelect,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '星期',
              key: 'week',
              type: JVXETypes.select,
              options:[],
              dictCode:"week",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '备注',
              key: 'remark',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/inspect/insRule/add",
          edit: "/inspect/insRule/edit",
          queryById: "/inspect/insRule/queryById",
          insRuleLine: {
            list: '/inspect/insRule/queryInsRuleLineByMainId'
          },
          insRuleDuty: {
            list: '/inspect/insRule/queryInsRuleDutyByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.insRuleLineTable.dataSource=[]
        this.insRuleDutyTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.insRuleLine.list, params, this.insRuleLineTable)
          this.requestSubTableData(this.url.insRuleDuty.list, params, this.insRuleDutyTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          insRuleLineList: allValues.tablesValue[0].tableData,
          insRuleDutyList: allValues.tablesValue[1].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>
